import router from "@/router";
import { defineStore, storeToRefs } from "pinia";
import { ref } from "vue";
import {
  usePersistStore,
  // userGroupStore,
  useDeskStore,
} from "@/stores";
import {
  employeeLoginService,
  modifyPasswordService,
  // resetPasswordSercice,
} from "@/api/employeeApi";
export const useUserStore = defineStore("noPersist", () => {
  //const groupStore = userGroupStore();
  const persisterStore = usePersistStore();
  const deskStore = useDeskStore();
  const {
    getAllRfids,
    getAvailableRfids,
  } = deskStore;
  const { userInfo } = storeToRefs(persisterStore);
  //登录表单数据
  const loginFormData = ref({
    employeeId: "",
    //identity: "",
    password: "",
  });
  const loginForm = ref();
  //用户登录请求
  const loginService = async () => {
    const res = await employeeLoginService(loginFormData.value);
    userInfo.value = res.data;
    ElMessage.success(res.message);
    getAllRfids();
    getAvailableRfids();
    router.push("/");
  };
  //用户登录
  const login = async () => {
    await loginForm.value.validate();
    loginService();
    loginFormData.value = {
      employeeId: "",
      //identity: "",
      password: "",
    };
  };
  //用户退出:不涉及到请求
  const exit = () => {
    ElMessageBox.confirm("你确定要退出吗?", "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        userInfo.value = {}; //清除用户信息
        router.push("/login");
        ElMessage.success("退出成功");
      })
      .catch(() => {
        ElMessage.success("取消退出");
      });
  };
  //用户密码的重置
  const rePwdForm = ref();
  const rePwdFormData = ref({
    oldPwd: "",
    //identity: "",
    employeeId: "",
    newPwd: "",
    rePwd: "",
  });
  //用户重置密码的表单验证
  const rePwdRules = {
    employeeId: [
      { required: true, message: "请输入员工工号", trigger: "blur" },
      {
        pattern: /^\d{7}$/,
        message: "员工工号长度必须是7位的数字",
        trigger: "blur",
      },
    ],
    // identity: [
    //   { required: true, message: "请输入员工身份证号", trigger: "blur" },
    //   {
    //     pattern:
    //       /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/,
    //     message: "您的身份证号输入有误",
    //     trigger: "blur",
    //   },
    // ],
    oldPwd: [{ required: true, message: "请输入老密码", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
    newPwd: [{ required: true, message: "请输入新密码", trigger: "blur" }],
    rePwd: [
      { required: true, message: "请再次输入新密码", trigger: "blur" },
      {
        validator: (rule, value, callback) => {
          if (value !== rePwdFormData.value.newPwd) {
            callback(new Error("两次输入的密码不一致"));
          } else {
            callback();
          }
        },
        trigger: "blur",
      },
    ],
  };
  //用户重置密码表单的清空
  const clearRePwdFormData = () => {
    rePwdFormData.value = {
      oldPwd: "",
      //identity: "",
      employeeId: "",
      newPwd: "",
      rePwd: "",
    };
  };
  //用户重置密码请求
  const modifyPassword = async () => {
    const res = await modifyPasswordService({
      employeeId: rePwdFormData.value.employeeId,
      newPwd: rePwdFormData.value.newPwd,
    });
    userInfo.value.token = "";
    ElMessage.success(res.message);
  };
  //员工重置密码表单的提交
  const submitRePwd = async () => {
    await rePwdForm.value.validate();
    //开始验证能否修改
    console.log(rePwdFormData.value);
    if (
      rePwdFormData.value.employeeId == userInfo.value.employeeId &&
      //rePwdFormData.value.identity == userInfo.value.identity &&
      rePwdFormData.value.oldPwd == userInfo.value.password
    ) {
      ElMessageBox.confirm(
        "您确定要修改密码为:" + rePwdFormData.value.newPwd + "?",
        "温馨提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          modifyPassword();
          router.push("/login");
          clearRePwdFormData();
        })
        .catch(() => {
          ElMessage.success("取消修改密码");
          clearRePwdFormData();
        });
    } else {
      ElMessage.error("您输入的信息有误,请联系管理员");
    }
  };
  // //用户重置密码请求
  // const forgetPwd = async () => {
  //   const res = await resetPasswordSercice(loginFormData.value);
  //   ElMessage.success(res.message);
  // };
  // //用户忘记密码
  // const clearPwd = () => {
  //   forgetPwd();
  //   loginFormData.value = { employeeId: "", password: "" };
  // };
  return {
    rePwdForm,
    rePwdFormData,
    rePwdRules,
    loginFormData,
    loginForm,
    login,
    exit,
    clearRePwdFormData,
    submitRePwd,
    // clearPwd,
  };
});
